const Sequelize = require('sequelize')
const Config = require('./config')

// 实例化sequelize
const sequelize = new Sequelize(Config)

// 导入模型统一管理
const TopCategory = sequelize.import('./models/TopCategory.js')
const SecCategory = sequelize.import('./models/SecCategory.js')
const Item = sequelize.import('./models/Item.js')
const Hero = sequelize.import('./models/Hero.js')
const Score = sequelize.import('./models/Score.js')
const Skill = sequelize.import('./models/Skill.js')
const Tips = sequelize.import('./models/Tips.js')
const Cooperate = sequelize.import('./models/Cooperate.js')
const SecHero = sequelize.import('./models/SecHero.js')
const HeroItem = sequelize.import('./models/HeroItem.js')
const Article = sequelize.import('./models/Article.js')
const Ad = sequelize.import('./models/Ad.js')
const Aditem = sequelize.import('./models/Aditem.js')
const AdminUser = sequelize.import('./models/AdminUser.js')

// 表间关联关系

//一个一级分类有多个子分类
SecCategory.belongsTo(TopCategory, { foreignKey: 'Superiorid', targetKey: 'id' })

Article.belongsTo(SecCategory, { foreignKey: 'Superiorid', targetKey: 'id' })

//一个子分类有多个英雄,一个英雄也有多个子分类
SecCategory.belongsToMany(Hero, { through: { model: SecHero }, foreignKey: 'secid' });
Hero.belongsToMany(SecCategory, { through: { model: SecHero }, foreignKey: 'heroid' });

//一个英雄只有一个综合分
Hero.belongsTo(Score, { foreignKey: 'score_id', targetKey: 'id' })

//一个英雄有多个技能
Hero.hasMany(Skill);

//一个英雄一套顺风套
Item.belongsToMany(Hero, { through: { model: HeroItem }, foreignKey: 'itemid' })
Hero.belongsToMany(Item, { through: { model: HeroItem }, foreignKey: 'heroid' })

//一个英雄只有一套技巧
Hero.belongsTo(Tips, { foreignKey: 'tips_id', targetKey: 'id' })

//一个英雄有很多伙伴
Hero.hasMany(Cooperate)
//一个广告有很多图片
Ad.hasMany(Aditem)
// 自动生成模型
TopCategory.sync({ force: false });
SecCategory.sync({ force: false });
Article.sync({ force: false });
Hero.sync({ force: false });
Item.sync({ force: false });
Score.sync({ force: false });
Skill.sync({ force: false });
Tips.sync({ force: false });
Cooperate.sync({ force: false });
SecHero.sync({ force: false });
HeroItem.sync({ force: false });
Ad.sync({ force: false });
Aditem.sync({ force: false });
AdminUser.sync({ force: false });

module.exports = { TopCategory, SecCategory, Article, Item, Hero, Score, Skill, Tips, Cooperate, SecHero, HeroItem, Ad, Aditem, AdminUser }